package com.wego.controller;

import com.oracle.wls.shaded.org.apache.bcel.generic.ClassGen;
import com.wego.bean.Notification;
import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.common.minio.MinioUtil;
import com.wego.entity.domain.Category;
import com.wego.entity.domain.Goods;
import com.wego.entity.query.GoodsQuery;
import com.wego.ex.GlobalException;
import com.wego.service.CategoryService;
import com.wego.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @className: GoodsController
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2023/07/11 10:15
 **/
@Controller
@RequestMapping("manager/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MinioUtil minioUtil;

    @GetMapping("/list")
    public @ResponseBody TableBean<Goods> list(GoodsQuery goodsQuery,Model model) {
        //构造动态查询条件
        PageBean<Goods> pageBean = goodsService.selectPage(goodsQuery);
        List<Goods> goods = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean<Goods> tableBean = new TableBean<>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(goods);
        tableBean.setCount(count);
        return tableBean;
    }

    @GetMapping("/details")
    public String details(Long id, Model model) {
        Goods goods = goodsService.selectByPrimaryKey(id);
        model.addAttribute("goods", goods);
        return "manager/goods_details";
    }

    @GetMapping("/updateState")
    public Void updateState(Long id, Model model) {
        Goods goods = goodsService.selectByPrimaryKey(id);
        Integer state = goods.getState();
        if (state == null && id == null) {
            throw new GlobalException(400, "执行修改状态时提交的参数不正确", "list");
        }
        //修改状态的值
        goods.setState(state == 1 ? 0 : 1);
        goods.setUpdateTime(LocalDateTime.now());
        goodsService.updateByPrimaryKeySelective(goods);
        return null;
    }

    @GetMapping("/openAddUpdate")
    public String openAddUpdate(Long id, Model model) {
        if (id != null) {
            Goods goods = goodsService.selectByPrimaryKey(id);
            model.addAttribute("goods", goods);
        }
        return "manager/goods_add_update";
    }



    @PostMapping("/addUpdate")
    public @ResponseBody StateBean addUpdate(Goods goods) {
        goods.setUpdateTime(LocalDateTime.now());
        boolean res;
        String msg;
        if(goods.getSpecs() != null && goods.getSpecs().equals("")){
            goods.setSpecs(null);
        }
        if(goods.getInfo() != null && goods.getInfo().equals("")){
            goods.setInfo(null);
        }
        Long id = goods.getId();
        if (id != null) {
            //修改
            res = goodsService.updateByPrimaryKeySelective(goods);
            msg = res ? "修改成功！" : "修改失败";
        } else {
            //添加
            goods.setCreateTime(LocalDateTime.now());
            res = goodsService.insertSelective(goods);
            msg = res ? "添加成功！" : "添加失败";
        }
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }

    @GetMapping("/batchDelete")
    public @ResponseBody StateBean batchDelete(String ids) {
        System.out.println(ids);
        boolean res = goodsService.batchDelete(ids);
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }


}
